import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_sign_entity.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/utils/toast_utils.dart';

class ApplyListModel extends ChangeNotifier {
  ListSignEntity? listSignEntity;

  /// 获取报名列表
  Future<ListSignEntity?> postListSign(int currentPage, String page) async {
    Map<String, dynamic> param = {
      "currentPage": currentPage,
      'pageSize': 15,
      'param': page
    };

    ResultData resultData = await Api.postListSign(param);
    if (resultData.isSuccess) {
      if (currentPage == 1) {
        listSignEntity = ListSignEntity.fromJson(resultData.data['data']);
      } else if (currentPage > 1) {
        listSignEntity!.content!.addAll(ListSignEntity.fromJson(resultData.data['data']).content!);
      }
    } else {
      toast.show(resultData.data["message"]);
    }
    notifyListeners();
  }

  /// 立即报名
  Future<bool> postGoCollectSign(String id) async {
    Map<String, dynamic> param = {"id": id};
    ResultData resultData = await Api.postGoCollectSign(param);
    if (resultData.isSuccess) {
      toast.show(resultData.data["message"]);
      return true;
    } else {
      toast.show(resultData.data["message"]);
      return false;
    }
  }

  /// 更新 某一条 数据
  upSignData(String collectId, String status, String totalSign) {
    listSignEntity!.content!.forEach((element) {
      if (element.collectId == collectId) {
        element.status = status;
        element.totalSign = totalSign;
        notifyListeners();
      }
    });
  }
}
